(function($) {
    /*
     * Native FullScreen JavaScript API 
     * Assumes Mozilla naming conventions instead of W3C for now
     */
    var fullScreenApi = {
            supportsFullScreen : false,
            isFullScreen : function() {
                return false;
            },
            requestFullScreen : function() {},
            cancelFullScreen : function() {},
            fullScreenEventName : '',
            prefix : '',
            fullScreenElement : null
        }, 
        browserPrefixes = 'webkit moz o ms khtml'.split(' ');

    // check for native support
    if ( typeof document.cancelFullScreen != 'undefined') {
        fullScreenApi.supportsFullScreen = true;
    } else {
        // check for fullscreen support by vendor prefix
        for (var i = 0, iLen = browserPrefixes.length; i < iLen; i++) {
            
            fullScreenApi.prefix = browserPrefixes[i];
            
            if ( typeof document[fullScreenApi.prefix + 'CancelFullScreen'] != 'undefined') {
                fullScreenApi.supportsFullScreen = true;
                break;
            }
        }
    }

    //---------------------------------------------------------------------------------------------------------

    // update methods to do something useful
    if (fullScreenApi.supportsFullScreen) {        
        fullScreenApi.fullScreenEventName = fullScreenApi.prefix + 'fullscreenchange';
        /**
         * 网页是否处理全屏状态.
         */
        fullScreenApi.isFullScreen = function() {
            switch (this.prefix) {
                case '':
                    return document.fullScreen;
                case 'webkit':
                    return document.webkitIsFullScreen;
                default:
                    return document[this.prefix + 'FullScreen'];
            }
        };        
        /**
         * 开启网页全屏。
         * @param {Element} el   : 网页全屏元素，即将该元素布满全屏.
         * @param {Function} callback : 元素全屏后的回调函数.
         */
        fullScreenApi.requestFullScreen = function(el, callback) {
            fullScreenApi.fullScreenElement = el;
            (this.prefix === '') ? el.requestFullScreen() : el[this.prefix + 'RequestFullScreen']();
            if( callback ){
                callback(el);
            }
        };        
        /**
         * 取消全屏状态.
         * @param {Function} callback : 取消全屏后的回调函数.
         */
        fullScreenApi.cancelFullScreen = function(callback) {
            fullScreenApi.fullScreenElement = null;
            (this.prefix === '') ? document.cancelFullScreen() : document[this.prefix + 'CancelFullScreen']();
            if( callback ){
                callback();
            }
        };
    }

    //---------------------------------------------------------------------------------------------------------

    // jQuery plugin
    if ( typeof $ != 'undefined') {        
        $.fn.requestFullScreen = function() {
            return this.each(function() {
                var el = $(this);
                if (fullScreenApi.supportsFullScreen) {
                    fullScreenApi.requestFullScreen(el);
                }
            });
        };
    }
        
    ujs.define('fullScreenApi', 'ujs.browser', function(){
        return fullScreenApi;
    });    
})($);